﻿using BoardBroker;
using ComLib.ComType;
using ComUIControl;
using CoreBroker;
using KWindow.KPageCore;
using LogLib;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IPCPages
{
    public partial class PageTakePhoto : PageControlBase
    {
        private BoardControl _board = BoardControl.Instance;
        private PictureBox pictureBox1 = null;
        private Image[] CountdownImages = new Image[3];
      
        private CoreBroker.CapturerBroker capturerBroker1;
        private Taker _taker = null;
        private TakePhotoOption _option = null;
        private ProcessTakePhoto _process = null;
        public PageTakePhoto()
        {
            InitializeComponent();
            _process = ProcessTakePhoto.Instance;
            SetProcessBase(_process);
            _process.CountdownUpdated += OnTakePhotoCountdownUpdated;
        }

        private void GetControls()
        {
            pictureBox1 = GetControl<PictureBox>("pictureBox1") as PictureBox;
            CountdownImages[0] = GetImage("Countdown1");
            CountdownImages[1] = GetImage("Countdown2");
            CountdownImages[2] = GetImage("Countdown3");
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            base.InitCtrls();
            GetControls();           
        }

        /// <summary>
        /// 初始化窗体数据
        /// </summary>
        public override void InitData()
        {
            pictureBox1.Visible = false;          
            this.BackgroundImage = null;
            capturerBroker1 = Process.GetValue<CoreBroker.CapturerBroker>("Capturer");
            if (capturerBroker1 == null)
            {
                Log.WriteAsync("获取相机失败!");
                MsgBox.Show("获取相机失败!");               
                return;
            }
            _taker = _process.Taker;
            _option = _process.Option;         
            _taker.AddTaskAbortedEvent(OnTaskAborted);
            //_taker.TakingCompleted += OnTakingCompletedReported;          
        }

        private void OnTaskAborted(object sender, TakingAbortedEventArgs e)
        {
            if (this.InvokeRequired)
            {
                TakingAbortedEventHandler handler
                    = new TakingAbortedEventHandler(OnTaskAborted);
                this.Invoke(handler, new object[] { sender, e });
            }
            _board.StopWorking();
            capturerBroker1.StopCapturer();
            NotifyClientTakePhotoAborted(e.AbortCode, e.AbortReason);
            ClearData();
            Process.ToHome();
        }

        private void NotifyClientTakePhotoAborted(int code, string reason)
        {
            if (Process.Job != null)
            {
                Process.Job.Abort(reason, code);
            }
        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            base.UpdateData();
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
            pictureBox1.Visible = false;
            if (capturerBroker1 != null)
            {
                capturerBroker1.StopCapturer();
            }               
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
        }

        /// <summary>
        /// 开始拍照
        /// </summary>
        public override void StartWork()
        {
           base.StartWork();
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        protected void OnTakePhotoCountdownUpdated(int sec)
        {
            if (this.InvokeRequired)
            {
                Action<int> outdelegate = new Action<int>(OnTakePhotoCountdownUpdated);
                this.Invoke(outdelegate, new object[] { sec });
                return;
            }
            string countdownSec = sec.ToString();
            Speak(countdownSec);
            int index = sec - 1;
            pictureBox1.Visible = true;
            pictureBox1.Image = CountdownImages[index];
        }

    }
}
